/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.bugeden.netdisk.vfs;

import java.io.Serializable;

/**
 *
 * @author bug-eden
 */
public class FolderX implements DiskInterface,Serializable {

    static final long serialVersionUID = 423534L;

    private  String name;

    private  String parentPath ="";

    private  java.util.Hashtable< String,DiskInterface >  includeList;

    public FolderX( String name )
    {
        includeList = new java.util.Hashtable< String,DiskInterface >();
        
        this.name = new String(name);
    }



    public String getFolderName()
    {
        return name;
    }

    public boolean addComponent( DiskInterface comp )
    {
        if( comp==null )
            return false;
        if( includeList.containsKey(comp.getName()))
            return false;
        comp.appendPath( this.getPath() );
        includeList.put( comp.getName() , comp );
        return true;
    }

    public void appendPath(String path) {
        this.parentPath = path;
    }

    public  java.util.Enumeration getComponentEnum(  )
    {
        return includeList.elements();
    }

    private DiskInterface getComponentByName ( String name )
    {
        if( !includeList.containsKey(name) )
            return null;
        return includeList.get(name);
    }

    public int size()
    {
        return includeList.size();
    }

    public FolderX getFolderX( String name )
    {
        DiskInterface dis = this.getComponentByName(name);
        if( ! (dis instanceof FolderX))
            return null;
        return (FolderX) dis;
    }

    public DiskInterface getComponent( String path )
    {


        String fileName[] = path.split(PathSeparator);

        DiskInterface diskIf = this;
        int i;
        if(fileName.length<=0 || !fileName[0].equals( this.name ))
            return null;
        for( i= 1;i<fileName.length; i++ )
        {
            if( diskIf instanceof org.bugeden.netdisk.vfs.FolderX )
            {
                diskIf =  ((org.bugeden.netdisk.vfs.FolderX)diskIf ).getComponentByName( fileName[i] );
            }
            else
                break;
        }

        if( i< fileName.length)
            return null;
        return diskIf;
    }

    public String getName() {
        return this.name;
    }

    public String getParentPath() {
        return this.parentPath;
    }

    public String getPath() {
        return this.parentPath+this.name+DiskInterface.PathSeparator;
    }


}
